#include "protocol.h"

Protocol::Protocol()
{

}

//=====================================================================
QString Protocol::setCMD(QString key, QString value)
{
    return QString("set-%1:%2").arg(key).arg(value);
}
QString Protocol::getCMD(QString key)
{
    return QString("get-%1").arg(key);
}
QString Protocol::ctlCMD(QString key, QString value)
{
    if(value.isEmpty())
        return QString("ctl-%1").arg(key);
    else
        return QString("ctl-%1:%2").arg(key).arg(value);
}

//=====================================================================
QStringList months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
QString Protocol::cmd_setTime(QDateTime time)
{
    time = time.addSecs(2);    //矫正时间差
    QString timeStr = months.at(time.date().month() - 1) + time.toString(" dd yyyy,hh:mm:ss");
    QString cmdStr = ctlCMD(Ctl_Time,timeStr);
    return cmdStr;
}

//=====================================================================
//获取wifi信息
QString Protocol::cmd_getWifi()
{
    QStringList cmdList;
    cmdList.append(getCMD(Key_WifiName));
    cmdList.append(getCMD(Key_WifiPwd));
    cmdList.append(getCMD(Key_WifiAutoConn));
    return cmdList.join(";\n");
}
//设置wifi所有信息
QString Protocol::cmd_setWifi(QString name,QString pwd,bool flag)
{
    QStringList cmdList;
    cmdList.append(cmd_setWifiName(name));
    cmdList.append(cmd_setWifiPwd(pwd));
    cmdList.append(cmd_setWifiAutoConn(flag));
    return cmdList.join(";\n");
}
//设置wifi名字
QString Protocol::cmd_setWifiName(QString name)
{
    return setCMD(Key_WifiName,name);
}
//设置wifi密码
QString Protocol::cmd_setWifiPwd(QString pwd)
{
    return setCMD(Key_WifiPwd,pwd);
}
//设置wifi自动连接
QString Protocol::cmd_setWifiAutoConn(bool flag)
{
    return setCMD(Key_WifiAutoConn,QString::number((int)flag));
}
//控制wifi连接、断开
QString Protocol::cmd_ctlWifiConn(bool flag)
{
    return ctlCMD(Ctl_WifiConnState,QString::number((int)flag));
}

//=====================================================================
//获取mqtt所有信息
QString Protocol::cmd_getMqtt()
{
    QStringList cmdList;
    cmdList.append(getCMD(Key_MqttPath));
    cmdList.append(getCMD(Key_MqttPort));
    cmdList.append(getCMD(Key_MqttName));
    cmdList.append(getCMD(Key_MqttPwd));
    cmdList.append(getCMD(Key_MqttAutoConn));
    return cmdList.join(";\n");
}
//设置mqtt所有信息
QString Protocol::cmd_setMqtt(QString path,QString port,QString name,QString pwd,bool flag)
{
    QStringList cmdList;
    cmdList.append(cmd_setMqttPath(path));
    cmdList.append(cmd_setMqttPort(port));
    cmdList.append(cmd_setMqttName(name));
    cmdList.append(cmd_setMqttPwd(pwd));
    cmdList.append(cmd_setMqttAutoConn(flag));
    return cmdList.join(";\n");
}
//设置mqtt地址，如IP地址
QString Protocol::cmd_setMqttPath(QString path)
{
    return setCMD(Key_MqttPath,path);
}
//设置mqtt端口号
QString Protocol::cmd_setMqttPort(QString port)
{
    return setCMD(Key_MqttPort,port);
}
//设置mqtt用户名
QString Protocol::cmd_setMqttName(QString name)
{
    return setCMD(Key_MqttName,name);
}
//设置mqtt密码
QString Protocol::cmd_setMqttPwd(QString pwd)
{
    return setCMD(Key_MqttPwd,pwd);
}
//设置mqtt自动连接
QString Protocol::cmd_setMqttAutoConn(bool flag)
{
    return setCMD(Key_MqttAutoConn,QString::number((int)flag));;
}

//=====================================================================
//获取状态切换时间信息
QString Protocol::cmd_getSwitchTime()
{
    QStringList cmdList;
    cmdList.append(getCMD(Key_WaitTime));
    cmdList.append(getCMD(Key_OutTime_1));
    cmdList.append(getCMD(Key_OutTime_2));
    cmdList.append(getCMD(Key_OutTime_3));
    cmdList.append(getCMD(Key_OutTime_4));
    cmdList.append(getCMD(Key_ChangePeriod));
    cmdList.append(getCMD(Key_ChangeTime));
    return cmdList.join(";\n");
}
//设置状态切换时间
QString Protocol::cmd_setSwitchTime(int waitTime,
                                    int outTime_1,int outTime_2,int outTime_3,int outTime_4,
                                    int changePeriod,int changeTime)
{
    QStringList cmdList;
    cmdList.append(cmd_setWaitTime(waitTime));
    cmdList.append(cmd_setOutTime_1(outTime_1));
    cmdList.append(cmd_setOutTime_2(outTime_2));
    cmdList.append(cmd_setOutTime_3(outTime_3));
    cmdList.append(cmd_setOutTime_4(outTime_4));
    cmdList.append(cmd_setChangePeriod(changePeriod));
    cmdList.append(cmd_setChangeTime(changeTime));
    return cmdList.join(";\n");
}

QString Protocol::cmd_setWaitTime(int time)
{
    return setCMD(Key_WaitTime,QString::number(time));;
}
QString Protocol::cmd_setOutTime_1(int time)
{
    return setCMD(Key_OutTime_1,QString::number(time));;
}
QString Protocol::cmd_setOutTime_2(int time)
{
    return setCMD(Key_OutTime_2,QString::number(time));;
}
QString Protocol::cmd_setOutTime_3(int time)
{
    return setCMD(Key_OutTime_3,QString::number(time));;
}
QString Protocol::cmd_setOutTime_4(int time)
{
    return setCMD(Key_OutTime_4,QString::number(time));;
}
QString Protocol::cmd_setChangePeriod(int time)
{
    return setCMD(Key_ChangePeriod,QString::number(time));;
}
QString Protocol::cmd_setChangeTime(int time)
{
    return setCMD(Key_ChangeTime,QString::number(time));;
}
